package com.ruoyi.web.controller.system;


import cn.hxzy.common.util.RandomId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hxzy.system.entity.TdBusinessAffiliation;
import com.hxzy.system.service.TdBusinessAffiliationService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.enums.DataSourceType;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-24
 */

@RestController
@RequestMapping("/tdBusinessAffiliation")
@DataSource(DataSourceType.SYSTEM)
public class TdBusinessAffiliationController {


    @Autowired
    private TdBusinessAffiliationService tdBusinessAffiliationService;


    @GetMapping("/getBusinessList")
    public AjaxResult getBusinessList(){
        List<TdBusinessAffiliation> list = tdBusinessAffiliationService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("getBusinessInfo/{id}")
    public AjaxResult getBusinessInfo(@PathVariable String id){
        TdBusinessAffiliation byId = tdBusinessAffiliationService.getById(id);
        return AjaxResult.success(byId);
    }

    @PutMapping("/updateBusiness")
    public AjaxResult updateBusiness(@RequestBody TdBusinessAffiliation tdBusinessAffiliation){
        UpdateWrapper<TdBusinessAffiliation>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",tdBusinessAffiliation.getId());
        boolean update = tdBusinessAffiliationService.update(tdBusinessAffiliation, updateWrapper);
        if (update){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }


    @PostMapping("/addBusiness")
    public AjaxResult addBusiness(@RequestBody TdBusinessAffiliation tdBusinessAffiliation){
        tdBusinessAffiliation.setId(RandomId.getBusinessId());
        boolean save = tdBusinessAffiliationService.save(tdBusinessAffiliation);
        if (save){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    @DeleteMapping("deleteBusiness/{id}")
    public AjaxResult deleteBusiness(@PathVariable String id){
        boolean b = tdBusinessAffiliationService.removeById(id);
        if (b){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @GetMapping("/getBusinessByName/{name}")
    public AjaxResult getBusinessByName(@PathVariable String name){
        QueryWrapper<TdBusinessAffiliation>queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<TdBusinessAffiliation> list = tdBusinessAffiliationService.list(queryWrapper);
        return AjaxResult.success(list);
    }



}

